package org.ezplatform.office.questionnaire.dao;

import java.util.List;

import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.office.questionnaire.entity.QusetionnaireAnswerStatus;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**   
* @author 作者: wujs
* @version 创建时间：2017年12月10日 上午10:47:08 
* 类说明 
*/
@Repository("qusetionnaireAnswerStatusDao")
public interface QusetionnaireAnswerStatusDao extends JpaBaseDao<QusetionnaireAnswerStatus,String>{
	
	@Query("from QusetionnaireAnswerStatus where id in (?1)")
	List<QusetionnaireAnswerStatus> findByIds(List<String> ids);
	
	@Query("from QusetionnaireAnswerStatus where createdBy = ?1 and questionnaireId=?2")
	QusetionnaireAnswerStatus findAnswerStatus(String userId,String quesId);
	
	@Query("from QusetionnaireAnswerStatus where questionnaireId=?1 and status='1' order by grade desc")
	List<QusetionnaireAnswerStatus> findAnswerStatusListGradeDesc(String quesId);
	
	@Query("select count(1) from QusetionnaireAnswerStatus where questionnaireId=?1 and status='1'")
	int getAnswerNumByQuesId(String quesId);
	
	@Query("from QusetionnaireAnswerStatus  where   status = ?1 and questionnaireId=?2 ")
	List<QusetionnaireAnswerStatus> getAnswerStatus(String status,String quesId);
	
	@Query("select count(1) from QusetionnaireAnswerStatus where status = ?1 and questionnaireId=?2")
	int getAnswerNumByQuesId(String status,String quesId);
	
	@Modifying
	@Query(nativeQuery = true, value = "delete from oa_qusetionnaire_answer_status where questionnaire_id =?1")
	int deleteStatusByQuesId(String id);
	
	@Modifying
	@Query(nativeQuery = true, value = "delete from oa_qusetionnaire_answer_status where questionnaire_id in (?1)")
	int deleteStatusByQuesId(List<String> list);
	
	@Modifying
	@Query(nativeQuery = true, value = "update oa_qusetionnaire_answer_status set status = ?1 where id in (?2)")
	int updateStatusById(String status,List<String> list);
	
	@Modifying
	@Query(nativeQuery = true, value = "update oa_qusetionnaire_answer_status set status = '3', back_reason=?1 where id in (?2) and status = '1'")
	int replyQusetionnaireByIds(String backReason, List<String> ids);
	
	//关闭时将草稿状态的答卷变成提交
	@Modifying
	@Query(nativeQuery = true, value = "update oa_qusetionnaire_answer_status set status = '1' where questionnaire_id = ?1 and status ='2'")
	int updateStatusToSubmit(String quesId);
	
	//当问卷结束时间小于当前时间将草稿状态的答卷变成提交
	@Modifying
	@Query(nativeQuery = true, value = "update oa_qusetionnaire_answer_status set status ='1' where status ='2' and questionnaire_id in (select id from oa_questionnaire where end_date < SYSDATE())")
	int updateStatusToSubmitByDate();
	
	@Query("select questionnaireId from QusetionnaireAnswerStatus where status = ?1 and questionnaireId in (?2) and createdBy = ?3")
	List<String> getQuesIdStatus(String status,List quesId, String userId);
	
	@Query("select questionnaireId from QusetionnaireAnswerStatus where questionnaireId in (?1)")
	List<String> getQuesId(List quesId);
}
